<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: pi_clickandbuy_recurring_debit.php 93 2013-09-21 12:04:50Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 93 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 12:04:50 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright © since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	© 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	© 2002-2003 osCommerce - www.oscommerce.com
* 	© 2003     nextcommerce - www.nextcommerce.org
* 	© 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * @category  PayIntelligent
 * @package   PayIntelligent_ClickandBuy
 * @copyright (C) 2010 PayIntelligent GmbH  <http://www.payintelligent.de/>
 * @license   GPLv2
 */

require('includes/application_top.php');
$language= $_SESSION['language'];

include('../lang/'.$_SESSION['language'].'/admin/pi_clickandbuy.php');		

require(DIR_FS_CATALOG.DIR_WS_CLASSES.'class.clickandbuy.php');
$cab = new clickandbuy();			

$shopOrderID = $_GET['oID'];
!empty($_GET['externalID']) ? $externalID = $_GET['externalID'] : $externalID = substr(md5(uniqid(rand())),0,12);
!empty($_GET['intPlaces']) ? $intPlaces = $_GET['intPlaces'] : $intPlaces = '0';	
!empty($_GET['decPlaces']) ? $decPlaces = $_GET['decPlaces'] : $decPlaces = '00';	
!empty($_GET['actionSave']) ? $actionSave = true : $actionSave = false;	

if(!empty($_GET['itemDesc1'])) $itemDesc1 = $_GET['itemDesc1'];

$authentication = $cab->getCabSettings('clickandbuy_recurring');

$cabOrderDetails = $db->db_query("SELECT * FROM ".DB_PREFIX."picab_orders WHERE shopOrderID = '".$shopOrderID."'");

$postAmount = (float)$intPlaces.'.'.$decPlaces;
		
$amount = (float)$cabOrderDetails->fields['amount'];
	      
if($actionSave) {
	if ($postAmount <= 0.00) {
		$postAmount = 0;
		$messageBox = 'ERROR';
		$errors[] = $errorDescription = 'invalid_recurring_amount';
	} 
			
	if(!$errors) {
		$items = array();
		$details = array();
				
		$details['amount'] = $postAmount;
		$details['currency'] = $cabOrderDetails->fields['currency'];
		$details['recurringAuthorizationID'] = $cabOrderDetails->fields['authorizationID'];				
		$details['externalID'] = $externalID;
		$items[1]['itemType'] = 'item1Text';	
		$items[1]['textItemDescription'] = $itemDesc1;
				
		$requestResult = $cab->payRequestRecurring($authentication,$details,$shippingType = '',$shippingAddress = '',$billingType = '',$billingAddress = '',$items);
		$debited = number_format(($cabOrderDetails->fields['debited'] + $postAmount),2);
	
		if ($requestResult['values']['transaction']['transactionStatus'] == 'SUCCESS' OR $requestResult['values']['transaction']['transactionStatus'] == 'IN_PROGRESS') {
			$messageBox = 'SUCCESS';					

			$transactionID = $requestResult['values']['transaction']['transactionID'];
			$transactionStatus = $requestResult['values']['transaction']['transactionStatus'];

			$db->db_query("UPDATE ".DB_PREFIX."picab_orders SET debited = '".$debited."', modified = NOW() WHERE shopOrderID ='".$shopOrderID."'");
			$db->db_query("INSERT INTO ".DB_PREFIX."picab_transactions (id,shopOrderID,transactionID,externalID,transactionType,description,amount,currency,paid,status,created,modified) VALUES (NULL,'".$shopOrderID."','".$transactionID."','".$externalID."','recurring','".$itemDesc1."',".$postAmount.",'".$cabOrderDetails->fields['currency']."',1,'".$transactionStatus."',NOW(),NOW())");
		} else {
			$messageBox = 'ERROR';
			$errorDescription = $requestResult['values']['detail']['errorDetails']['description'];
		}					
	}
}			

$cabOrderDetails = $db->db_query("SELECT * FROM ".DB_PREFIX."picab_orders WHERE shopOrderID ='".$shopOrderID."' ");

$transactions = $db->db_query("SELECT SUM(amount)as recurringTotalAmount FROM ".DB_PREFIX."picab_transactions WHERE shopOrderID = '".$shopOrderID."' AND  transactionType = 'recurring'");
$recurringTotalAmount = $transactions->fields['recurringTotalAmount'];
?>
<table border="0" width="100%" cellspacing="0" cellpadding="2">
	<tr>
		<td>
			<table border="0" width="100%" cellspacing="0" cellpadding="2" height="40">
				<tr>
					<td class="pageHeading"><?php echo CLICKANDBUY_ORDER_CLICKANDBUY.' '.CLICKANDBUY_ORDER_DETAILS_RECURRING; ?></td>
					<a class="button cab_button" href="pi_clickandbuy_details.php?oID=<?php echo $shopOrderID; ?>">
						<?php echo CLICKANDBUY_ORDER_DETAILS_BACK; ?>
					</a>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td>
		<?php 
			if ($messageBox == 'SUCCESS') {
				$showMessage = $cab->showMessageSuccess(CLICKANDBUY_ORDER_RECURRING_DEBIT_SUCCESSFUL. ' ('.CLICKANDBUY_ORDER_DETAILS_CLICKANDBUY_STATUS.': '.$transactionStatus.').');
				echo '<div class="clickandbuy_msg">'.$showMessage.'</div>';
			} elseif ($messageBox == 'ERROR') {
				$showMessage = $cab->showMessageError(CLICKANDBUY_ORDER_RECURRING_DEBIT_ERROR.' '.$errorDescription);
				echo '<div class="clickandbuy_msg">'.$showMessage.'</div>';
			}
			?>									                
			<table border="0" width="100%" cellspacing="0" cellpadding="2">
				<tr>
					<td>
						<table cellspacing="0" cellpadding="0" border="0" width="98%">
							<tr>
								<td width="50%">
									<table cellspacing="0" cellpadding="5" border="0" width="400">
										<tr>
											<td valign="top" class="infoBoxHeading" colspan="2">
												<b><?php echo CLICKANDBUY_ORDER_RECURRING_DEBIT; ?>: </b>
											</td>                      
										</tr>
										<tr>
											<td class="infoBoxContent" height="20"><?php echo CLICKANDBUY_ORDER_DETAILS_EXTERNALID; ?>:</td>
											<td class="infoBoxContent"><?php echo $cabOrderDetails->fields['externalID']; ?></td>
										</tr>
										<tr>
											<td class="infoBoxContent" height="20" width="120"><?php echo CLICKANDBUY_ORDER_RECURRING_AUTHORIZATION_AMOUNT; ?>:</td>
											<td class="infoBoxContent"><?php echo $cabOrderDetails->fields['recAmount'].' '.$cabOrderDetails->fields['currency']; ?></td>
										</tr>
										<tr>
											<td class="infoBoxContent" height="20" width="120"><?php echo CLICKANDBUY_ORDER_DETAILS_TOTAL_AMOUNT; ?>:</td>
											<td class="infoBoxContent"><?php echo $recurringTotalAmount.' '.$cabOrderDetails->fields['currency']; ?></td>
										</tr>
										<tr>
											<td class="infoBoxContent" height="20" width="120"><?php echo CLICKANDBUY_ORDER_DETAILS_AMOUNT; ?>:</td>
											<td class="infoBoxContent">
												<input name="intPlaces" type="text"  value="<?php echo $intPlaces; ?>" size="3" maxlength="12" class="editinput intPlaces">,
												<input name="decPlaces" type="text"  value="<?php echo $decPlaces; ?>" size="2" maxlength="2" class="editinput decPlaces"> 
												<?php echo $cabOrderDetails->fields['currency']; ?>
											</td>
										</tr>
										<tr>
											<td class="infoBoxContent" height="20" width="120"><?php echo CLICKANDBUY_ORDER_DETAILS_DESCRIPTION; ?>:</td>
											<td class="infoBoxContent"><input type="text" value="<?php echo $itemDesc1; ?>" name="itemDesc1" maxlength="255" size="25" class="editinput itemDesc1"></td>
										</tr>
										<tr>
											<td class="infoBoxContent" height="20" width="120">&nbsp;</td>
											<td class="infoBoxContent">
												<a onclick="return getInputs()" class="button credit_href cab_button" href="">
													<?php echo CLICKANDBUY_ORDER_RECURRING_DEBIT_NOT; ?>
												</a>
												<script type="text/javascript">
													function getInputs() {
														var url = '<?php echo 'pi_clickandbuy_recurring_debit.php?oID='.$shopOrderID.'&actionSave=true&externalID='.$externalID; ?>';
														url += '&intPlaces='+$('.intPlaces').val();
														url += '&decPlaces='+$('.decPlaces').val();
														url += '&itemDesc1='+$('.itemDesc1').val();
														$('.credit_href').attr('href', url);
													}
												</script>
											</td>
										</tr>
									</table>
								</td>
							</tr>	
						</table>
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>